﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GuiLibrary.layouts;
using GuiLibrary.Components;
using Microsoft.Xna.Framework;

namespace GuiLibrary.Layouts
{
    public class ListLayoutMultiPanel: ListLayout
    {
        #region Constructors
        public ListLayoutMultiPanel(Panel parentPanel, int[] margins, float[] weight, bool isVertical, IOnElementSelected onComponentSelected, IOnCancel onCancel, IOnElementPointed onComponentPointed) :
            base(parentPanel, margins, weight, isVertical, onComponentSelected, onCancel, onComponentPointed) 
        { }
        public ListLayoutMultiPanel(Panel parentPanel, int[] margins, bool isVertical, IOnElementSelected onComponentSelected, IOnCancel onCancel, IOnElementPointed onComponentPointed) :
            base(parentPanel, margins, isVertical, onComponentSelected, onCancel, onComponentPointed) { }
        #endregion
        #region Functionality
        public override Component getCurrentComponent()
        {
            Component currentHighLvlComp = getComponents()[pointedElement];
            if (currentHighLvlComp is Panel) 
            {
                Panel currentHighLvlPanel = (Panel)currentHighLvlComp;
                return currentHighLvlPanel.layout.getCurrentComponent();
            }
            return currentHighLvlComp;
        }
        public override void moveCursor(Layout.Cursor cursor)
        {
            bool jumpingPanel = true;
            bool up = false, down = false;
            Component currentHighLvlComp = getComponents()[pointedElement];
            Panel panelHighLvl = currentHighLvlComp is Panel ? (Panel)currentHighLvlComp : null;
            switch (cursor) 
            {
                case Cursor.DOWN:
                    down = true;
                    goto case Cursor.RIGHT;
                case Cursor.RIGHT:
                    if (isVertical == down) //if it's moving in the direction of this list (down if vertical, right if not)
                    {
                        if (panelHighLvl != null) //if it's a panel
                        {
                            if (panelHighLvl.canMove(cursor)) //if it can move inside that panel
                                jumpingPanel = false;   //then don't jump
                        }
                        if (jumpingPanel && pointedElement < getComponents().Count - 1) //if must jump and it's not the last element
                            changePointedComponent(pointedElement + 1); //jump
                        else if (panelHighLvl != null) //else if it's a panel
                        {
                            panelHighLvl.moveCursor(cursor); //move inside
                        }
                    }
                    else //if it's moving in another direction (right if vertical, down if not)
                    {
                        if (panelHighLvl != null) //if it's a panel
                            panelHighLvl.moveCursor(cursor); //move inside
                    }
                    break;
                case Cursor.UP:
                    up = true;
                    goto case Cursor.LEFT;
                case Cursor.LEFT:
                    if (isVertical == up) //if it's moving in the direction of this list (up if vertical, left if not)
                    {
                        if (panelHighLvl != null) //if it's a panel
                        {
                            if (panelHighLvl.canMove(cursor)) //if it can move inside that panel
                                jumpingPanel = false;   //then don't jump
                        }
                        if (jumpingPanel && pointedElement >= 0) //if must jump and it's not the first element
                        {
                            changePointedComponent(pointedElement - 1); //jump
                        } else if (panelHighLvl != null) //else if it's a panel
                        {
                            panelHighLvl.moveCursor(cursor); //move inside
                        }
                    }
                    else //if it's moving in another direction (left if vertical, up if not)
                    {
                        if (panelHighLvl != null) //if it's a panel
                            panelHighLvl.moveCursor(cursor); //move inside
                    }
                    break;
                default:
                    checkEnterCancel(cursor);
                    break;
            }
        }
        public override Vector2 getComponentSize(Component component)
        {
            foreach (Component highLvlComp in getComponents()) 
            {
                if (component == highLvlComp)
                    //if it's an up lvl component
                    return base.getComponentSize(component);
                if (component is Panel)
                {
                    Panel highLvlPanel = (Panel)component;
                    if(highLvlPanel.components.Contains(component)) //if it's a low lvl component
                        return highLvlPanel.layout.getComponentSize(component);
                }
            }
            return new Vector2();
        }
        #endregion
    }
}
